package com.wanxi.education.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.user.CUserChangePasswordVo;
import com.wanxi.education.model.vo.user.*;
import com.wanxi.education.service.LoginService;
import com.wanxi.education.service.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: YanZhengYi
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@RestController
@Api(tags = "前后端登录页面以及用户修改密码")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisService redisService;

    @PostMapping("/back/admin/login")
    @ApiOperation("管理员登录，传入账户和密码")
    public CommonResult adminLogin(@RequestBody(required = false) LoginVo loginVo) {
        Map<String, String> data = loginService.adminLogin(loginVo);
        return CommonResult.success(data);

    }

    @PostMapping("/back/BUser/account/login")
    @ApiOperation("合作机构账户登录，传入手机号、密码和验证码")
    public CommonResult bUserAccountLogin(@RequestBody BUserLoginVo bUserLoginVo) {
        log.info("B端用户账号密码登陆 数据"+ JSONObject.toJSONString(bUserLoginVo));
        if(StringUtils.isBlank(bUserLoginVo.getCodePicId())){
            return CommonResult.failure("验证码id错误");
        }
        String redisCode = redisService.get("codePicId-" + bUserLoginVo.getCodePicId());
        if (!StringUtils.equals(redisCode,bUserLoginVo.getPicCode()) || StringUtils.isBlank(bUserLoginVo.getPicCode())){
            return CommonResult.failure("验证码错误");
        }
        Map<String, String> map = loginService.BLoginByPass(bUserLoginVo);

        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failure("密码错误");
        }

        return CommonResult.success(map);
    }
    @PostMapping("/back/BUser/phone/login")
    @ApiOperation("合作机构手机登录，传入手机号和验证码")
    public CommonResult bUserAccountLogin(@RequestBody BUserPhoneLoginVo bUserLoginVo) {
        log.info("B端用户手机验证码登陆 数据"+ JSONObject.toJSONString(bUserLoginVo));
        String redisCode = redisService.get("codeTel-" + bUserLoginVo.getPhone());
        if (!StringUtils.equals(redisCode,bUserLoginVo.getCode())|| StringUtils.isBlank(bUserLoginVo.getCode())){
            return CommonResult.failure("验证码错误");
        }
        Map<String, String> map =  loginService.BLoginByCode(bUserLoginVo);
        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failure("该账号不存在/或已注销");
        }
        return CommonResult.success(map);
    }

//    @PostMapping("/back/BUser/phone/getCode")
//    @ApiOperation("合作机构账户登录，传入手机号，得到验证码")
//    public CommonResult bUserAccountLogin(String phone) {
//        return CommonResult.success("返回一个验证码");
//    }
//
//    @PostMapping("/front/BUser/phone/getCode")
//    @ApiOperation("学员账户登录，传入手机号，得到验证码")
//    public CommonResult cUserAccountLogin(String phone) {
//        return CommonResult.success("返回一个验证码");
//    }

    @PostMapping("/front/CUser/phone/login")
    @ApiOperation("学员手机登录，传入手机号和验证码")
    public CommonResult cUserAccountLogin(@RequestBody CUserPhoneLoginVo cUserLoginVo) {
        log.info("B端用户手机验证码登陆 数据"+ JSONObject.toJSONString(cUserLoginVo));
        String redisCode = redisService.get("codeTel-" + cUserLoginVo.getPhone());
        if (!StringUtils.equals(redisCode,cUserLoginVo.getCode())|| StringUtils.isBlank(cUserLoginVo.getCode())){
            return CommonResult.failure("验证码错误");
        }
        Map<String, String> map =  loginService.CLoginByCode(cUserLoginVo);
        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failure("该账号不存在/或已注销");
        }
        return CommonResult.success(map);
    }

    @PostMapping("/front/CUser/account/login")
    @ApiOperation("学员账户登录，传入手机号、密码和验证码")
    public CommonResult bUserAccountLogin(@RequestBody CUserLoginVo cUserLoginVo) {
        log.info("B端用户账号密码登陆 数据"+ JSONObject.toJSONString(cUserLoginVo));
        Map<String, String> map = loginService.CLoginByPass(cUserLoginVo);
        if (ObjectUtils.isEmpty(map)){
            return CommonResult.failure("密码错误");
        }
        return CommonResult.success(map);
    }

    @PostMapping("/front/CUser/account/changePassword")
    @ApiOperation("学员修改密码")
    public CommonResult cUserChangePassword(@RequestBody CUserChangePasswordVo cUserChangePasswordVo){
        log.info("C端用户更改密码 数据" + JSONObject.toJSONString(cUserChangePasswordVo));
        String redisCode = redisService.get("codeTel-" + cUserChangePasswordVo.getPhone());
        if (!StringUtils.equals(redisCode,cUserChangePasswordVo.getCode())|| StringUtils.isBlank(cUserChangePasswordVo.getCode())){
            return CommonResult.failure("验证码错误");
        }
        String result = loginService.changePassword(cUserChangePasswordVo);
        if (!"success".equals(result)){
            return CommonResult.failure("重置失败，请重试");
        }
        return CommonResult.success("重置成功");
    }

    @GetMapping("/phoneCode")
    @ApiOperation("用户手机号获取验证码")
    @ApiParam(value = "电话号码", required = true)
    private void phoneCode(@RequestParam("phone") String phone) throws Exception {
        log.info("用户手机号获取验证码 数据"+ JSONObject.toJSONString(phone));
        loginService.getPhoneCode(phone);
    }

    @GetMapping("/pictureCode")
    @ApiOperation("用户获取图片验证码")
    private CommonResult pictureCode() throws Exception {
        Map<String, String> pictureCode = loginService.getPictureCode();
        return CommonResult.success(pictureCode);
    }
}
